var make_base_auth = function(user, password) {
	var tok = user + ':' + password;
	var hash = Base64.encode(tok);
	return "Basic " + hash;
};
Ext.onReady(function() {
	
	
	Ext.create('Ext.window.Window', {
		title: 'Login',
		layout:'fit',
		resizable:false,
		constrain :true,
		items:[
		       
		       {
				xtype: 'form',
				id:"form",
				method:'GET',
				bodyPadding: 5,
				width: 350,
				
				// Fields will be arranged vertically, stretched to full width
				layout: 'anchor',
				defaults: {
					anchor: '100%'
				},
			
				// The fields
				items: [
				      
						{
							xtype:'textfield',
							fieldLabel: 'Username',
							value: Ext.util.Cookies.get("username"),
							name: 'username',
							allowBlank: false
						},
						{
							xtype:'textfield',
							fieldLabel: 'Password',
							value:Ext.util.Cookies.get("password"),
							inputType:'password',
							name: 'password',
							allowBlank: false
						},
						{
							xtype:'button',
							anchor: '100%',
							text: 'Submit',
							formBind: true,
							handler: function(){
								myForm = this.up('form').getForm();
								myFormValues = myForm.getFieldValues();
								myForm.submit({
									onSuccess:   function(form, action) {
										Ext.util.Cookies.set("username",myFormValues.username);
										Ext.util.Cookies.set("password",myFormValues.password);
										Ext.Msg.alert('Success',"YEAH");
									},
									url:'manager/userroles',
									headers :{
										Accept: ("application/json"),
										Authorization : make_base_auth(myFormValues.username,myFormValues.password),
									},
									
									success: function(form, action) {
										Ext.util.Cookies.set("username",myFormValues.username);
										console.log(action);
										Ext.Msg.alert('Success', action.result.msg);
			                        },
			                        failure: function(form, action) {
			                        	console.log(form);
			                        	console.log(action);
			                        	Ext.Msg.alert('Failed',"Doh");
			                        }
								});
							}
						}
				
						],
					renderTo: Ext.getBody(),
			}
		]
	}).show();
		
});